package com.dengdai.applibrary;

import android.app.Activity;
import android.app.Application;

import com.dengdai.applibrary.utils.CrashHandlerUtils;
import com.dengdai.applibrary.utils.FileManagerUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @说明 Application基类
 * @作者 LY
 * @时间 15/12/15 13:36
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public class BaseApplication extends Application {
    /**
     * 全局Context
     */
    public static BaseApplication instance;

    public static List<Activity> activityList = new LinkedList<Activity>();


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        FileManagerUtils.getInstance().setFolderName("APPUTILS");
        //日志捕获类
        CrashHandlerUtils.getInstance().init(this);
    }

    /**
     * 获取全局变量
     *
     * @return
     */
    public static BaseApplication getAppContext() {
        if (instance == null) {
            instance = new BaseApplication();
        }
        return instance;
    }

    /**
     * 添加Activity到容器中
     */
    public static void addActivity(Activity activity) {
        activityList.add(activity);
    }

    /**
     * 删除Activity
     *
     * @param activity
     */
    public static void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    /**
     * 遍历所有Activity并finish
     */
    public static void exit() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        System.exit(0);
    }

}
